package com.whc.gulimall.product.app;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.whc.common.utils.PageUtils;
import com.whc.common.utils.R;
import com.whc.gulimall.product.entity.SkuInfoEntity;
import com.whc.gulimall.product.service.SkuInfoService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * sku信息
 *
 * @author wanghaochen
 * @email angaoen@outlook.com
 * @date 2022-03-05 00:18:53
 */
@RestController
@RequestMapping("product/skuinfo")
public class SkuInfoController {

    /**
     * sku信息 Service
     */
    @Resource
    private SkuInfoService skuInfoService;

    /**
     * 列表
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = skuInfoService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 根据skuId获取sku信息
     *
     * @param skuId skuId
     * @return 统一返回结果
     */
    @GetMapping("/info/{skuId}")
    public R info(@PathVariable("skuId") Long skuId) {
        SkuInfoEntity skuInfo = skuInfoService.getById(skuId);
        return R.ok().put("skuInfo", skuInfo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:skuinfo:save")
    public R save(@RequestBody SkuInfoEntity skuInfo) {
        skuInfoService.save(skuInfo);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:skuinfo:update")
    public R update(@RequestBody SkuInfoEntity skuInfo) {
        skuInfoService.updateById(skuInfo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:skuinfo:delete")
    public R delete(@RequestBody Long[] skuIds) {
        skuInfoService.removeByIds(Arrays.asList(skuIds));

        return R.ok();
    }

    /**
     * 通过skuId获取到skuName
     *
     * @param skuIds skuId列表
     * @return 统一返回结果
     */
    @PostMapping("/list/skuName")
    public R listSkuNamesBySkuIds(@RequestBody List<Long> skuIds) {
        Map<Long, String> maps = skuInfoService.listSkuNamesBySkuIds(skuIds);
        return R.ok().put("data", maps);
    }

    /**
     * 通过skuId获取到skuName
     *
     * @param skuIds skuId列表
     * @return 统一返回结果
     */
    @PostMapping("/list")
    public R listSkuInfoBySkuIds(@RequestBody List<Long> skuIds) {
        return R.ok().setData(skuInfoService.list(new QueryWrapper<SkuInfoEntity>().in("sku_id", skuIds)));
    }

    /**
     * 通过skuId列表获取到最新价格
     *
     * @param skuIds skuIds
     * @return 统一返回结果
     */
    @PostMapping("/skuPrices")
    public R listSkuPricesByIds(@RequestBody List<Long> skuIds) {
        return R.ok().setData(skuInfoService.listSkuPricesByIds(skuIds));
    }
}
